package com.metalrain.alm.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.*;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import com.metalrain.alm.AndroidLightManagerApplication;
import com.metalrain.alm.R;
import com.metalrain.alm.service.LCA_Service;

public class LightListActivity extends Activity  {
    AlertDialog progress = null;
    BroadcastReceiver serviceUpdated = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateUI();
        }


    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cam_hue);
    }

    @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(serviceUpdated, new IntentFilter(LCA_Service.BROADCAST_HUE_SERVICE_UPDATED));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progress!=null) {
            progress.dismiss();
        }
    }

    /**
     * Update the UI based on the service;
     */
    private void updateUI() {
        if (progress != null) {
            progress.dismiss();
            progress = null;
        }
        final LCA_Service lcaService = AndroidLightManagerApplication.getLcaService();
        if (lcaService == null) {
            progress = ProgressDialog.show(this, "Service Connecting","Please Wait", true);
            return;
        } else {
            switch (lcaService.getState()) {
                case ERROR:

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Error");
                    builder.setMessage(lcaService.getErrorMessage());
                    builder.setCancelable(true);
                    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            lcaService.retry();
                        }
                    });

                    progress = builder.create();
                    progress.show();

                    break;
                case AUTHENTICATING:
                    progress = ProgressDialog.show(this, "Authentication Required",
                            "Please press the button on the Hue Controller to pair with the App", true);
                    break;
                case INITIALIZING:
                    progress = ProgressDialog.show(this, "Initializing",
                            "Service Initializing", true);
                    break;
                case LOCATING:
                    progress = ProgressDialog.show(this, "Locating Phillips Hue Controller",
                            "Locating", true);
                    break;
                default:
                    break;
            }
        }
    }



}
